package com.cloudcc.mobile.view.test;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.view.test.TestStepActivity.OnSendSMSListener;

/**  测试第一步骤 输入手机号码
* @Description: TODO() 
* @author wangzhiqiang
* @date 2016-3-2 下午3:34:30 
*  
*/
public class Step2Fragment extends BaseStepFragment {

	
	@Bind(R.id.test_done)
	Button btn_done;
	
	@Bind(R.id.btn_sms_get)
	Button btn_sms_get;
	
	@Bind(R.id.test_tip1)
	TextView test_sended_tv;
	
	@Bind(R.id.et_test_phone)
	EditText et_test_phone;
	
	
	@Override
	public void init() {
		super.init();
		initPhone();
		initEditText();
		
	}
	
	private void initEditText() {
		et_test_phone.setOnEditorActionListener(new EditText.OnEditorActionListener() {  
			  
		    @Override  
		    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
		        if (actionId == EditorInfo.IME_ACTION_DONE) {  
		            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
		            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
		            clickCommit();
		            return true;    
		        }  
		        return false;  
		    }  
		      
		});
		
	}
	
	
	private void initPhone(){
		String verPhone =((TestStepActivity)getActivity()).getTestPhoneMobile();
		if (!TextUtils.isEmpty(verPhone)) {
			test_sended_tv.setText("验证码已经发送到"+verPhone+",请查收");
			startTimerTask();
		}
		
	}
	
	private Timer timer;
	private TimerTask timerTask;
	/** 
	* 开始计时
	*/ 
	protected void startTimerTask() {
		timer =new Timer();
		timerTask =new ChatTask();
		timer.schedule(timerTask, 10,1*1000);
	}
	/** 
	* 停止计时
	*/ 
	private void stopTimerTask() {
		btn_sms_get.setEnabled(true);
		btn_sms_get.setText("重新获取验证码");
		cancelTimer();
	}
	
	private class ChatTask extends TimerTask {
		
		private int  secondCount =60*2;

		@Override
		public void run() {
			secondCount--;
			mParentActivity.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					if (secondCount<=0) {
						stopTimerTask();
					}else{
						btn_sms_get.setEnabled(false);
						btn_sms_get.setText("已发送("+secondCount+")");
					}
				}
			});
		}

	}
	private void cancelTimer() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (timerTask != null) {
			timerTask.cancel();
			timerTask = null;
		}
	}
	
	@OnClick(R.id.test_done)
	public void clickCommit(){
		String inputNumber = et_test_phone.getText().toString().trim();
		if (TextUtils.isEmpty(inputNumber)) {
			Toast.makeText(mContext, "请输入验证码!", 0).show();
			return;
		}
		boolean isOk = ((TestStepActivity)getActivity()).verifyVerNumber(inputNumber);
		if (!isOk) {
			Toast.makeText(mContext, "验证码不正确!", 0).show();
			return;
		}
		//进入选中帐号服务
		MoveNextStep();
	}
	
	@OnClick(R.id.btn_sms_get)
	public void clickGetSMS(){
		mParentActivity.resetSMSListener();
		mParentActivity.sendVerSMS(mParentActivity.getTestPhoneMobile(),new OnSendSMSListener() {
			
			@Override
			public void onSuccess() {
				mParentActivity.resetSMSListener();
				showToast("验证码发送成功,请注意查收...");
				startTimerTask();
				
			}
			
			@Override
			public void onFailure() {
				mParentActivity.resetSMSListener();
				showToast("验证码发送失败,请重试...");
			}
		});
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		cancelTimer();
	}
	
	
	@Override
	public int getLayoutId() {
		return R.layout.test_step2;
	}
	
	

}
